package com.szsh.aiot.hsm.service.impl;

import com.google.gson.JsonObject;
import com.szsh.aiot.common.core.domain.AjaxResult;
import com.szsh.aiot.common.core.domain.RequestDto;
import com.szsh.aiot.hsm.mapper.INetprotectSwitchMapper;
import com.szsh.aiot.hsm.domain.netprotect.INetprotectSwitch;
import com.szsh.aiot.hsm.service.NetworkProtectService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class NetworkProtectServiceImpl implements NetworkProtectService {
    private static final Logger LOGGER = LoggerFactory.getLogger(NetworkProtectServiceImpl.class);

    private final INetprotectSwitchMapper netprotectSwitchMapper;

    public NetworkProtectServiceImpl(INetprotectSwitchMapper netprotectSwitchMapper) {
        this.netprotectSwitchMapper = netprotectSwitchMapper;
    }

    /**
     * 网络防护开关查询接口
     * @param requestDto 请求参数
     * @return 处理结果
     */
    @Override
    public AjaxResult query(RequestDto requestDto) {
        // 请求参数
        JsonObject requestJsonObject = requestDto.getRequestJsonElement().getAsJsonObject();
        // 宽带账号
        String broadbandNO = requestJsonObject.get("broadbandNO").getAsString();

        INetprotectSwitch netprotectSwitch = netprotectSwitchMapper.selectINetprotectSwitchByBroadbandNO(broadbandNO);
        if (null == netprotectSwitch) {
            if (LOGGER.isErrorEnabled()) {
                LOGGER.error("根据宽带账号获取网络防护失败.{}", broadbandNO);
            }
        }
        return AjaxResult.success(netprotectSwitch);
    }

    /**
     * 网络防护开关修改接口
     * @param requestDto 请求参数
     * @return 处理结果
     */
    @Override
    public AjaxResult switchChange(RequestDto requestDto) {
        // 请求参数
        JsonObject requestJsonObject = requestDto.getRequestJsonElement().getAsJsonObject();
        // 宽带账号
        String broadbandNO = requestJsonObject.get("broadbandNO").getAsString();

        return null;
    }

    /**
     * 网络防护绑定手机号码接口
     * @param requestDto 请求参数
     * @return 处理结果
     */
    @Override
    public AjaxResult bindPhone(RequestDto requestDto) {
        // 请求参数
        JsonObject requestJsonObject = requestDto.getRequestJsonElement().getAsJsonObject();
        // 宽带账号
        String broadbandNO = requestJsonObject.get("broadbandNO").getAsString();
        // 绑定的手机号码
        String bindPhone = requestJsonObject.get("phone").getAsString();

        INetprotectSwitch netprotectSwitch = new INetprotectSwitch();

        netprotectSwitch.setBindPhone(bindPhone);
        netprotectSwitch.setBroadbandNO(broadbandNO);

        int cnt = netprotectSwitchMapper.updateINetprotectSwitch(netprotectSwitch);
        if (cnt > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("绑定失败");
    }

    /**
     * 网络防护绑定手机号码接口
     * @param requestDto 请求参数
     * @return 处理结果
     */
    @Override
    public AjaxResult unBindPhone(RequestDto requestDto) {
        // 请求参数
        JsonObject requestJsonObject = requestDto.getRequestJsonElement().getAsJsonObject();
        // 获取宽带账号
        String broadbandNO = requestJsonObject.get("broadbandNO").getAsString();

        INetprotectSwitch netprotectSwitch = new INetprotectSwitch();

        netprotectSwitch.setBindPhone("");
        netprotectSwitch.setBroadbandNO(broadbandNO);

        int cnt = netprotectSwitchMapper.updateINetprotectSwitch(netprotectSwitch);
        if (cnt > 0) {
            return AjaxResult.success();
        }
        return AjaxResult.error("解绑失败");
    }
}
